<?php
namespace application\datatypes;
use application\datatypes\DataType;
use application\utilities\Utility;
class Status extends DataType {
	public static $name = 'Status';
	public static $fields = array(array('columnType'=>'VARCHAR(20)'));
	
	public static function validate($field, $data, $results) {
		global $concerto;
		if($data['content_status']) {
			$results['db_fields']['content_status'] = $data['content_status'];
		}
		// If they're an admin make it approved, otherwise make it pending
		elseif(is_object($concerto->User) && $concerto->User->isAdmin()) {
			$results['db_fields']['content_status'] = 'approved';
		}
		else {
			$results['db_fields']['content_status'] = 'pending';
		}
		
		return $results;
	}
	
	public static function display($formElements) {
		if($formElements) {
			?>
			<fieldset class="single-field">
				<header><?=$formElements['display']?></header>
				<section class="inner">
				<?php if($description = $formElements['description']) : ?>
					<p class="form-description"><?=$description?></p>
				<?php endif;?>
				<?=$formElements['elements']['element']?>
				</section>
			</fieldset>
			<?php
		}
	}
	
	public static function build($field, $data) {
		if(Modulator()->runFilter('show_status_field', FALSE, $field)) {
			$value = $data['content_status'];
			$statuses = Modulator()->runFilter('valid_statuses', array('approved', 'pending', 'hidden', 'stub'));
			$formElements = array('description'=>$field->cf_description, 'column'=>$field->cf_column, 'display'=>$field->cf_display_name, 'data_type'=>$field->cf_data_type, 'elements'=>array()
												
			);
			
			$element = '<select name="content_status">';
			foreach($statuses as $status) {
				if($status == $value) {
					$element .= '<option value="'.$status.'" selected="selected">'.ucwords($status).'</option>';
				}
				else {
					$element .= '<option value="'.$status.'">'.ucwords($status).'</option>';
				}
			}
			$element .= '</select>';
			$formElements['elements']['element'] = $element;
			
			return $formElements;
		}
		return FALSE;
	
	}
	
	public static function editForm($field) {
		?>
		<div class="box-content">
			You can't edit an automatic field.
		</div>
			<?php
	}
	
	
}
?>